//
//  LoginViewController.m
//  DeliciousFood1
//
//  Created by brother on 15/10/21.
//  Copyright © 2015年 com.hxm. All rights reserved.
//
#import "AppDelegate.h"
#import "LoginViewController.h"
#import <AFNetworking.h>
#import "User.h"
#import "InfoViewController.h"


@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *iphone;
@property (weak, nonatomic) IBOutlet UITextField *password;

//用户登录
- (IBAction)login:(id)sender;
@end

@implementation LoginViewController

- (IBAction)login:(id)sender {
    if ([self.password.text isEqualToString:@""]||[self.iphone.text isEqualToString:@""]) {
        [[[UIAlertView alloc] initWithTitle:@"提示" message:@"用户名或密码为空！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
    }else
    {
        //注册信息
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        
        NSString *url = @"http://localhost:8888/thinkphp/index.php/home/info/loginUser";
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        dic[@"account"] = self.iphone.text;
        
        [manager GET:url parameters:dic success:^void(AFHTTPRequestOperation * operation, id  responseObject) {
            if ([(NSArray *)responseObject count] != 0) {
                User *user = [[User alloc] initWithDic:responseObject[0]];
                
                AppDelegate *application =  [[UIApplication sharedApplication] delegate] ;
                application.user = user;
                
                UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                InfoViewController *info = [storyBoard instantiateViewControllerWithIdentifier:@"info"];
                [info loginRefresh];
                
                
                [self dismissViewControllerAnimated:YES completion:nil];
            }else
            {
                [[[UIAlertView alloc] initWithTitle:@"提示" message:@"用户名或密码错误！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
            }
        } failure:^(AFHTTPRequestOperation *  operation, NSError * error) {
            [[[UIAlertView alloc] initWithTitle:@"提示" message:@"用户登录失败！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
        }];

    }
}
@end
